package com.minelx.statementsplus.split;

import com.minelx.statementsplus.MajorSql;
import com.minelx.statementsplus.common.LongList;

import java.util.List;
import java.util.function.Function;

import static java.util.stream.Collectors.toList;

public class LongArraySplitter implements MajorSplitter {
	private final long[] ids;

	private final int partitionLength;

	private final Function<long[], MajorSql> resultMapper;

	public LongArraySplitter(long[] ids, int partitionLength, Function<long[], MajorSql> resultMapper) {
		this.ids = ids;
		this.partitionLength = partitionLength;
		this.resultMapper = resultMapper;
	}

	@Override
	public List<MajorSql> split() {
		return LongList.split(ids, partitionsCount(ids.length, partitionLength)).stream()
				.map(resultMapper)
				.collect(toList());
	}

	public static int partitionsCount(int actualLength, int partitionLength) {
		return (int) Math.ceil(actualLength / (partitionLength * 1F));
	}
}
